package com.jstyle.jclifexd.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.jstyle.jclifexd.NetWork.NetWorkUtil;
import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.adapter.SquadlistAdapter;
import com.jstyle.jclifexd.daoManager.UserInfoDaoManager;
import com.jstyle.jclifexd.model.Activitities;
import com.jstyle.jclifexd.model.Activitvtiesmsg;
import com.jstyle.jclifexd.model.Activitvtiesteam;
import com.jstyle.jclifexd.model.Teamperformance;
import com.jstyle.jclifexd.model.UserInfo;
import com.jstyle.jclifexd.utils.RxBus;
import com.jstyle.jclifexd.utils.Utils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 战队列表
 */
public class SquadlistActivity extends BaseActivity {
    @BindView(R.id.activity_RecyclerView)
    RecyclerView activity_RecyclerView;
    @BindView(R.id.title)
    Button title;
    @BindView(R.id.embers_count)
    TextView emberscount;
    @BindView(R.id.bt_exitzhandui)
    Button bt_exitzhandui;
    SquadlistAdapter activitvtiesAdapter;
    Teamperformance teamperformance;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quadlist);
        ButterKnife.bind(this);
        activity_RecyclerView.setLayoutManager(new LinearLayoutManager(this));
        activitvtiesAdapter = new SquadlistAdapter(this);
        activity_RecyclerView.setAdapter(activitvtiesAdapter);
        if (null != getIntent() && null != getIntent().getSerializableExtra("teamperformance")) {
            teamperformance = (Teamperformance) getIntent().getSerializableExtra("teamperformance");
            title.setText(teamperformance.getTeamName().replace("\\n", ""));
            Querydata(teamperformance.getTeamId() + "");
        }
    }


    @OnClick({R.id.iv_back, R.id.bt_exitzhandui})
    public void onViewClicked(View view) {
        if (Utils.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.bt_exitzhandui:
                AlertDialog alertDialog = new AlertDialog.Builder(this, R.style.appalertdialog)
                        .setTitle(getString(R.string.tips))
                        .setMessage(getResources().getString(R.string.battleteamornot))
                        .setCancelable(true)
                        .setPositiveButton(getString(R.string.Confirm), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Exite();
                            }
                        })
                        .setNegativeButton(getResources().getString(R.string.cancel), null)
                        .create();
                alertDialog.show();
                break;
        }
    }


    /**
     * c查询数据
     *
     * @param teamId
     */
    private void Querydata(String teamId) {
        Map<String, Object> map = new HashMap<>();
        map.put("teamId", teamId);
        NetWorkUtil.getInstance().getJstyleApi().getqueryMember(map).
                subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Activitvtiesteam>() {
            @Override
            public void onSubscribe(Disposable d) {
            }
            @Override
            public void onNext(Activitvtiesteam activitvtiesData) {
                if (null != activitvtiesData.getData()) {
                    for (int i = 0; i < activitvtiesData.getData().size(); i++) {
                        if (NetWorkUtil.getUserId().equals(activitvtiesData.getData().get(i).getUid())) {
                            bt_exitzhandui.setVisibility(View.VISIBLE);
                        }
                    } }
                activitvtiesAdapter.SetData(activitvtiesData.getData());
                emberscount.setText(null == activitvtiesData.getData() ? "0" + getResources().getString(R.string.number) : activitvtiesData.getData().size() + getResources().getString(R.string.number));
            }
            @Override
            public void onError(Throwable e) {
            }
            @Override
            public void onComplete() {
            }
        });
    }


    /**
     * 退出战队
     */
    private void Exite() {
        Map<String, Object> map = new HashMap<>();
        map.put("uid", NetWorkUtil.getUserId());
        NetWorkUtil.getInstance().getJstyleApi().quitTeam(map).
                subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Activitvtiesmsg>() {
            @Override
            public void onSubscribe(Disposable d) {
            }
            @Override
            public void onNext(Activitvtiesmsg activitvtiesData) {
                showToast(getResources().getString(R.string.successdrawalteam));
                finish();
                RxBus.getInstance().post("finish");
            }
            @Override
            public void onError(Throwable e) {
            }
            @Override
            public void onComplete() {
            }
        });
    }
}
